import type { MockMethod } from 'vite-plugin-mock'
import type { API } from '../../src/types/api/response'
import type { Menu, MenuQuery, CreateMenu, UpdateMenu, MenuList } from '../../src/types/api/system/menu'
import { MenuType } from '../../src/types/api/system/menu'

// ------------ 模拟数据 ------------
/**
 * 预定义的真实菜单数据
 */
const menus: Menu[] = [
  {
    id: '1',
    name: '首页',
    parentId: null,
    type: MenuType.MENU,
    icon: 'DashboardOutlined',
    orderNum: 1,
    path: 'dashboard',
    component: 'dashboard/index',
    query: '',
    isExternal: false,
    isCache: true,
    isVisible: true,
    permission: 'dashboard',
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    statusLoading: false
  },
  {
    id: '2',
    name: '系统管理',
    parentId: null,
    type: MenuType.DIRECTORY,
    icon: 'SettingOutlined',
    orderNum: 2,
    path: 'system',
    component: '',
    query: '',
    isExternal: false,
    isCache: true,
    isVisible: true,
    permission: 'system',
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    statusLoading: false,
    children: [
      {
        id: '2-1',
        name: '用户管理',
        parentId: '2',
        type: MenuType.MENU,
        icon: 'UserOutlined',
        orderNum: 1,
        path: 'user',
        component: 'system/user/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'system:user:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false,
        children: [
          {
            id: '2-1-1',
            name: '用户查询',
            parentId: '2-1',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 1,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:user:query',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-1-2',
            name: '用户新增',
            parentId: '2-1',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 2,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:user:add',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-1-3',
            name: '用户修改',
            parentId: '2-1',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 3,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:user:edit',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-1-4',
            name: '用户删除',
            parentId: '2-1',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 4,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:user:remove',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-1-5',
            name: '用户导出',
            parentId: '2-1',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 5,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:user:export',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-1-6',
            name: '用户导入',
            parentId: '2-1',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 6,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:user:import',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          }
        ]
      },
      {
        id: '2-2',
        name: '角色管理',
        parentId: '2',
        type: MenuType.MENU,
        icon: 'TeamOutlined',
        orderNum: 2,
        path: 'role',
        component: 'system/role/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'system:role:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false,
        children: [
          {
            id: '2-2-1',
            name: '角色查询',
            parentId: '2-2',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 1,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:role:query',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-2-2',
            name: '角色新增',
            parentId: '2-2',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 2,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:role:add',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-2-3',
            name: '角色修改',
            parentId: '2-2',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 3,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:role:edit',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-2-4',
            name: '角色删除',
            parentId: '2-2',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 4,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:role:remove',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-2-5',
            name: '角色导出',
            parentId: '2-2',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 5,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:role:export',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          }
        ]
      },
      {
        id: '2-3',
        name: '菜单管理',
        parentId: '2',
        type: MenuType.MENU,
        icon: 'MenuOutlined',
        orderNum: 3,
        path: 'menu',
        component: 'system/menu/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'system:menu:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false,
        children: [
          {
            id: '2-3-1',
            name: '菜单查询',
            parentId: '2-3',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 1,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:menu:query',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-3-2',
            name: '菜单新增',
            parentId: '2-3',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 2,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:menu:add',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-3-3',
            name: '菜单修改',
            parentId: '2-3',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 3,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:menu:edit',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          },
          {
            id: '2-3-4',
            name: '菜单删除',
            parentId: '2-3',
            type: MenuType.BUTTON,
            icon: '',
            orderNum: 4,
            path: '',
            component: '',
            query: '',
            isExternal: false,
            isCache: false,
            isVisible: true,
            permission: 'system:menu:remove',
            status: 1,
            createTime: '2023-01-01 00:00:00',
            updateTime: '2023-01-01 00:00:00',
            statusLoading: false
          }
        ]
      },
      {
        id: '2-4',
        name: '部门管理',
        parentId: '2',
        type: MenuType.MENU,
        icon: 'AppstoreOutlined',
        orderNum: 4,
        path: 'dept',
        component: 'system/dept/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'system:dept:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false
      }
    ]
  },
  {
    id: '3',
    name: '系统监控',
    parentId: null,
    type: MenuType.DIRECTORY,
    icon: 'MonitorOutlined',
    orderNum: 3,
    path: 'monitor',
    component: '',
    query: '',
    isExternal: false,
    isCache: true,
    isVisible: true,
    permission: 'monitor',
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    statusLoading: false,
    children: [
      {
        id: '3-1',
        name: '在线用户',
        parentId: '3',
        type: MenuType.MENU,
        icon: 'UserOutlined',
        orderNum: 1,
        path: 'online',
        component: 'monitor/online/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'monitor:online:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false
      },
      {
        id: '3-2',
        name: '服务监控',
        parentId: '3',
        type: MenuType.MENU,
        icon: 'MonitorOutlined',
        orderNum: 2,
        path: 'server',
        component: 'monitor/server/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'monitor:server:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false
      }
    ]
  },
  {
    id: '4',
    name: '系统工具',
    parentId: null,
    type: MenuType.DIRECTORY,
    icon: 'ToolOutlined',
    orderNum: 4,
    path: 'tool',
    component: '',
    query: '',
    isExternal: false,
    isCache: true,
    isVisible: true,
    permission: 'tool',
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    statusLoading: false,
    children: [
      {
        id: '4-1',
        name: '代码生成',
        parentId: '4',
        type: MenuType.MENU,
        icon: 'CodeOutlined',
        orderNum: 1,
        path: 'gen',
        component: 'tool/gen/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'tool:gen:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false
      },
      {
        id: '4-2',
        name: '系统接口',
        parentId: '4',
        type: MenuType.MENU,
        icon: 'ApiOutlined',
        orderNum: 2,
        path: 'swagger',
        component: 'tool/swagger/index',
        query: '',
        isExternal: false,
        isCache: true,
        isVisible: true,
        permission: 'tool:swagger:list',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false
      }
    ]
  },
  {
    id: '5',
    name: '个人中心',
    parentId: null,
    type: MenuType.MENU,
    icon: 'UserOutlined',
    orderNum: 5,
    path: 'account',
    component: 'system/account/index',
    query: '',
    isExternal: false,
    isCache: false,
    isVisible: true,
    permission: 'system:account',
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    statusLoading: false
  },
  {
    id: '6',
    name: '外部链接',
    parentId: null,
    type: MenuType.DIRECTORY,
    icon: 'LinkOutlined',
    orderNum: 6,
    path: 'external',
    component: '',
    query: '',
    isExternal: false,
    isCache: true,
    isVisible: true,
    permission: 'external',
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    statusLoading: false,
    children: [
      {
        id: '6-1',
        name: 'Ant Design',
        parentId: '6',
        type: MenuType.MENU,
        icon: 'AntDesignOutlined',
        orderNum: 1,
        path: 'https://ant.design/index-cn',
        component: '',
        query: '',
        isExternal: true,
        isCache: false,
        isVisible: true,
        permission: '',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false
      },
      {
        id: '6-2',
        name: 'GitHub',
        parentId: '6',
        type: MenuType.MENU,
        icon: 'GithubOutlined',
        orderNum: 2,
        path: 'https://github.com',
        component: '',
        query: '',
        isExternal: true,
        isCache: false,
        isVisible: true,
        permission: '',
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
        statusLoading: false
      }
    ]
  }
];

// ------------ API 实现 ------------
/**
 * 获取菜单树
 */
const getMenuTreeApi: MockMethod = {
  url: '/api/system/menu/tree',
  method: 'get',
  response: ({ query }: { query: MenuQuery }): API.Response<MenuList> => {
    const { keyword, status } = query
    let filteredMenus = [...menus]

    if (keyword) {
      const filterByKeyword = (items: Menu[]): Menu[] => {
        return items.filter(item => {
          const match = item.name.includes(keyword)
          if (item.children) {
            item.children = filterByKeyword(item.children)
            return match || item.children.length > 0
          }
          return match
        })
      }
      filteredMenus = filterByKeyword(filteredMenus)
    }

    if (status !== undefined) {
      const filterByStatus = (items: Menu[]): Menu[] => {
        return items.filter(item => {
          const match = item.status === Number(status)
          if (item.children) {
            item.children = filterByStatus(item.children)
            return match || item.children.length > 0
          }
          return match
        })
      }
      filteredMenus = filterByStatus(filteredMenus)
    }

    return {
      code: 200,
      success: true,
      message: 'ok',
      data: {
        list: filteredMenus,
        total: filteredMenus.length
      }
    }
  }
}

/**
 * 获取菜单列表
 */
const getMenuListApi: MockMethod = {
  url: '/api/system/menu/list',
  method: 'get',
  response: ({ query }: { query: MenuQuery }): API.Response<MenuList> => {
    const { keyword, status } = query
    let filteredMenus = [...menus]

    if (keyword) {
      const filterByKeyword = (items: Menu[]): Menu[] => {
        return items.filter(item => {
          const match = item.name.includes(keyword)
          if (item.children) {
            item.children = filterByKeyword(item.children)
            return match || item.children.length > 0
          }
          return match
        })
      }
      filteredMenus = filterByKeyword(filteredMenus)
    }

    if (status !== undefined) {
      const filterByStatus = (items: Menu[]): Menu[] => {
        return items.filter(item => {
          const match = item.status === Number(status)
          if (item.children) {
            item.children = filterByStatus(item.children)
            return match || item.children.length > 0
          }
          return match
        })
      }
      filteredMenus = filterByStatus(filteredMenus)
    }

    return {
      code: 200,
      success: true,
      message: 'ok',
      data: {
        list: filteredMenus,
        total: filteredMenus.length
      }
    }
  }
}

/**
 * 获取菜单详情
 */
const getMenuDetailApi: MockMethod = {
  url: '/api/system/menu/:id',
  method: 'get',
  response: ({ params }: { params: { id: string } }): API.Response<Menu | null> => {
    const findMenu = (items: Menu[], id: string): Menu | null => {
      for (const item of items) {
        if (item.id === id) {
          return item
        }
        if (item.children) {
          const found = findMenu(item.children, id)
          if (found) {
            return found
          }
        }
      }
      return null
    }

    const menu = findMenu(menus, params.id)
    return {
      code: 200,
      success: true,
      message: 'ok',
      data: menu || null
    }
  }
}

/**
 * 创建菜单
 */
const createMenuApi: MockMethod = {
  url: '/api/system/menu',
  method: 'post',
  response: ({ body }: { body: CreateMenu }): API.Response<Menu> => {
    const newMenu: Menu = {
      ...body,
      id: String(menus.length + 1),
      createTime: new Date().toISOString(),
      updateTime: new Date().toISOString(),
      statusLoading: false
    }

    if (body.parentId) {
      const findParent = (items: Menu[]): boolean => {
        for (const item of items) {
          if (item.id === body.parentId) {
            if (!item.children) {
              item.children = []
            }
            item.children.push(newMenu)
            return true
          }
          if (item.children && findParent(item.children)) {
            return true
          }
        }
        return false
      }
      findParent(menus)
    } else {
      menus.push(newMenu)
    }

    return {
      code: 200,
      success: true,
      message: '创建成功',
      data: newMenu
    }
  }
}

/**
 * 更新菜单
 */
const updateMenuApi: MockMethod = {
  url: '/api/system/menu/:id',
  method: 'put',
  response: ({ body, query }: { body: UpdateMenu, query: { id: string } }): API.Response<null> => {
    const updateMenu = (items: Menu[]): boolean => {
      for (const item of items) {
        if (item.id === query.id) {
          Object.assign(item, {
            ...body,
            updateTime: new Date().toISOString()
          })
          return true
        }
        if (item.children && updateMenu(item.children)) {
          return true
        }
      }
      return false
    }

    updateMenu(menus)
    return {
      code: 200,
      success: true,
      message: '更新成功',
      data: null
    }
  }
}

/**
 * 删除菜单
 */
const deleteMenuApi: MockMethod = {
  url: '/api/system/menu',
  method: 'delete',
  response: ({ body }: { body: { ids: string[] } }): API.Response<null> => {
    const deleteMenus = (items: Menu[]): void => {
      for (let i = items.length - 1; i >= 0; i--) {
        const item = items[i]
        if (body.ids.includes(item.id)) {
          items.splice(i, 1)
        } else if (item.children) {
          deleteMenus(item.children)
        }
      }
    }

    deleteMenus(menus)
    return {
      code: 200,
      success: true,
      message: '删除成功',
      data: null
    }
  }
}

/**
 * 修改菜单状态
 */
const changeMenuStatusApi: MockMethod = {
  url: '/api/system/menu/:id/status',
  method: 'put',
  response: ({ body, query }: { body: { status: 0 | 1 }, query: { id: string }}): API.Response<null> => {
    const updateStatus = (items: Menu[]): boolean => {
      for (const item of items) {
        if (item.id === query.id) {
          item.status = body.status
          item.updateTime = new Date().toISOString()
          return true
        }
        if (item.children && updateStatus(item.children)) {
          return true
        }
      }
      return false
    }

    updateStatus(menus)
    return {
      code: 200,
      success: true,
      message: '状态修改成功',
      data: null
    }
  }
}

// ------------ 导出 ------------
export default [
  getMenuTreeApi,
  getMenuListApi,
  getMenuDetailApi,
  createMenuApi,
  updateMenuApi,
  deleteMenuApi,
  changeMenuStatusApi
] as MockMethod[] 